//
//  NewDataInfoViewController.m
//  wangzhe
//
//  Created by 李 佳伟 on 2018/3/9.
//  Copyright © 2018年 李 佳伟. All rights reserved.
//

#import "NewDataInfoViewController.h"
#import "JTCSM.h"
#import "MyDoubleLabelView.h"
#import "DBcommonUtils.h"

#import "DBCollectionViewCell.h"
#import "HeaderCollectionReusableView.h"
#import "DBModel.h"

#import "NewTongJiTableViewCell.h"

//获取屏幕宽度
#define ScreenWidth  [UIScreen mainScreen].bounds.size.width

//获取屏幕高度
#define ScreenHeight [UIScreen mainScreen].bounds.size.height

@interface NewDataInfoViewController ()<UIScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate, UITableViewDelegate,UITableViewDataSource>
{
    UIImageView *headerImageView;
    UIImageView *sanJiaImageView;
    CGFloat sanJiaoSize;
    CGFloat sanjiaoSpace;
    MyDoubleLabelView *leftMyInfoView;
    MyDoubleLabelView *rightMyInfoView;
    UIScrollView * bottomScrollview;
    NSString *mykeyword;
}

@property (nonatomic,strong) NSMutableArray *titlearray;
@property (nonatomic,strong) UITableView *tableview;
@property (nonatomic,strong) UIImageView *userLogoImageV;
@property (nonatomic,strong) UILabel *titleLabel;

//日历控件
@property (nonatomic,strong)UICollectionView * collcetionView;


//日历控件天数
@property (nonatomic,strong)NSMutableArray * daysArray;

//时间
@property (nonatomic,strong)NSString * date ;

@end

@implementation NewDataInfoViewController

- (void)gotBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [df isRemoveTarbar:YES];
    self.navigationController.navigationBar.hidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    //初始化当前时间
    [self getNowDate];
    //计算月份天数
    [self setMonthDays];
    
    headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KCONTROLLERWith, KCONTROLLERWith*0.5334)];
    headerImageView.image = [UIImage imageNamed:@"连续转入背景.png"];
    [self.view addSubview:headerImageView];
    headerImageView.userInteractionEnabled = YES;
    UIImage * firstImage = [UIImage imageNamed:@"erweimafanhui.png"];
    CGRect firstFrame = CGRectMake(20, 20, 50, 44);
    CGFloat width = firstFrame.size.width;
    CGFloat height = firstFrame.size.height;
    CGFloat imagesize = 20;
    UIButton * leftButton = [[UIButton alloc]initWithFrame:firstFrame];
    [leftButton setImage:firstImage forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(gotBack) forControlEvents:UIControlEventTouchUpInside];
    leftButton.imageEdgeInsets = UIEdgeInsetsMake((height-imagesize)/2, 0, (height-imagesize)/2, (width-imagesize));
    [self.view addSubview:leftButton];
//    140   150 110  0.35 0.375  0.275
    
    UILabel *titleLabel = [UILabel initWithFrame:CGRectMake(0, 20, KCONTROLLERWith, 44) text:@"连续转入" font:20 textAlignment:NSTextAlignmentCenter texcolor:RGBACOLOR(255, 255, 255, 1) backgroundColor:nil];
    [self.view addSubview:titleLabel];
    
    
    UIImageView *backImageV = [[UIImageView alloc]initWithFrame:CGRectMake(20, headerImageView.LC_height*0.35, KCONTROLLERWith-40, headerImageView.LC_height*0.375)];
    backImageV.image = [UIImage imageNamed:@"小背景.png"];
    [headerImageView addSubview:backImageV];
    
    rightMyInfoView = [[MyDoubleLabelView alloc]initWithFrame:CGRectMake(0, 0, backImageV.LC_width/2, backImageV.LC_height) titleFontsize:12 titlecolor:[df colorWithHexString:@"ffffff" alpha:0.7] contentFontsize:24 contentcolor:[df colorWithHexString:@"ffffff" alpha:1]];
    [backImageV addSubview:rightMyInfoView];
    [rightMyInfoView newsetvalueWithModel:@{KDoubleLabelTitle:@"已连续转入",KDoubleLabelContent:@"30天"}];
    
    leftMyInfoView = [[MyDoubleLabelView alloc]initWithFrame:CGRectMake(backImageV.LC_width/2, 0, backImageV.LC_width/2, backImageV.LC_height) titleFontsize:12 titlecolor:[df colorWithHexString:@"ffffff" alpha:0.7] contentFontsize:24 contentcolor:[df colorWithHexString:@"ffffff" alpha:1]];
    [backImageV addSubview:leftMyInfoView];
    [leftMyInfoView newsetvalueWithModel:@{KDoubleLabelTitle:@"最高纪录",KDoubleLabelContent:@"56天"}];
    
    [df lineViewWithWidthtofLeft:backImageV.LC_width/2-0.5 superView:backImageV spacing:14 color:RGBACOLOR(255, 255, 255, .7) lineWidth:1];
    
    
    
    
    UIImageView *leftImageV = [[UIImageView alloc]initWithFrame:CGRectMake((KCONTROLLERWith/2-headerImageView.LC_height*0.1125)/2, KVIEW_Y(backImageV)+headerImageView.LC_height*0.09, headerImageView.LC_height*0.1125, headerImageView.LC_height*0.1125)];
    leftImageV.image = [UIImage imageNamed:@"日历icon.png"];
    leftImageV.tag = 100;
    [headerImageView addSubview:leftImageV];
    leftImageV.userInteractionEnabled = YES;
    [df addTapGestureRecognizerWithTarget:self action:@selector(setSanJianFrame:) andaddView:leftImageV];
    
    UIImageView *rightImageV = [[UIImageView alloc]initWithFrame:CGRectMake(KCONTROLLERWith/2+(KCONTROLLERWith/2-headerImageView.LC_height*0.1125)/2, KVIEW_Y(backImageV)+headerImageView.LC_height*0.09, headerImageView.LC_height*0.1125, headerImageView.LC_height*0.1125)];
    rightImageV.image = [UIImage imageNamed:@"排行-icon.png"];
    rightImageV.tag = 101;
    [headerImageView addSubview:rightImageV];
    rightImageV.userInteractionEnabled = YES;
    [df addTapGestureRecognizerWithTarget:self action:@selector(setSanJianFrame:) andaddView:rightImageV];
    
    sanJiaoSize = headerImageView.LC_height*0.05;
    sanjiaoSpace = (KCONTROLLERWith/2-headerImageView.LC_height*0.05)/2;
    
    sanJiaImageView = [[UIImageView alloc]initWithFrame:CGRectMake(sanjiaoSpace, headerImageView.LC_height*0.96, sanJiaoSize, sanJiaoSize-headerImageView.LC_height*0.01)];
    sanJiaImageView.image = [UIImage imageNamed:@"三角形.png"];
    [headerImageView addSubview:sanJiaImageView];
    // Do any additional setup after loading the view.
    
    bottomScrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, KVIEW_Y(headerImageView), KCONTROLLERWith, KCONTROLLERHeight-headerImageView.LC_height)];
    bottomScrollview.delegate = self;
    bottomScrollview.bounces = NO;
    bottomScrollview.pagingEnabled = YES;
    [bottomScrollview setContentSize:CGSizeMake(KCONTROLLERWith*2, bottomScrollview.LC_height)];
    [self.view addSubview:bottomScrollview];
    [self setDBVC ];
}

// scrollview滚动的时候调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    if ([scrollView isKindOfClass:[UITableView class]]) {
        NSLog(@"------是列表---");
    }
    else {
        NSLog(@"------是滚动试图----");
        //    计算页码
        //    页码 = (contentoffset.x + scrollView一半宽度)/scrollView宽度
        CGFloat scrollviewW =  scrollView.frame.size.width;
        CGFloat x = scrollView.contentOffset.x;
        int page = (x + scrollviewW / 2) /  scrollviewW;
        NSLog(@"%f--%d",x,page);
        [self setScrollviewContentOffSetWithpage:page];
    }
    
}
- (void)setScrollviewContentOffSetWithpage:(int)page{
    [UIView animateWithDuration:0.3 animations:^{
        sanJiaImageView.frame = CGRectMake(sanjiaoSpace+page*(KCONTROLLERWith/2), headerImageView.LC_height*0.96, sanJiaoSize, sanJiaoSize-headerImageView.LC_height*0.01);
        
    } completion:^(BOOL finished) {
        page == 1?[self setTJVC]:[self setDBVC];
    }];
}
- (void)setSanJianFrame:(UITapGestureRecognizer *)tap{
    int number = (int)[tap view].tag-100;
    [self setScrollviewContentOffSetWithpage:number];
    bottomScrollview.contentOffset= CGPointMake(bottomScrollview.LC_width*number, 0);
}



- (void)setDBVC{
    //创建日历列表
    if (_collcetionView ==nil) {
        [self creatCollectionView];
    }
    
    
}
- (void)setTJVC{
    if (self.tableview == nil) {
        [self layoutTableView];

    }
   
}

#pragma 日历


#pragma mark 初始化当前时间
-(void)getNowDate
{
    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    
    NSString * date = [formatter stringFromDate:[NSDate date]];
    
    _date = [NSMutableString stringWithString:date];
    
    
}

#pragma mark 初始化日历
-(void)creatCollectionView
{
    
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init] ;
    //设置collectionView的滑动方向
    //UICollectionViewScrollDirectionVertical 垂直滑动   默认滑动方向
    //UICollectionViewScrollDirectionHorizontal 水平滑动
    //    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //在垂直方向  设置的是cell上下之间的最小间距 在水平方向  设置的是cell左右之间的最小间距
    flowLayout.minimumLineSpacing = 0 ;
    //在垂直方向  设置的是cell左右之间的最小间距 在水平方向  设置的是cell上下之间的最小间距
    flowLayout.minimumInteritemSpacing = 0 ;
    //设置collectionViewCell的大小
    
    flowLayout.itemSize = CGSizeMake(ScreenWidth/7, ScreenWidth/7+20);
    
    //设置头部视图的大小
    flowLayout.headerReferenceSize = CGSizeMake(ScreenWidth, ScreenWidth/7*2) ;
    
    //
    //第一个参数  位置大小
    //第二个参数  流布局对象 （UICollectionViewFlowLayout）
    
    
    _collcetionView  =[[ UICollectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, KCONTROLLERHeight-KCONTROLLERWith*0.5334) collectionViewLayout:flowLayout];
    
    [bottomScrollview addSubview:_collcetionView];
    
    
    _collcetionView.delegate = self ;
    _collcetionView.dataSource =self ;
    
    
    //    在这个地方进行cell复用注册
    [_collcetionView registerClass:[DBCollectionViewCell class] forCellWithReuseIdentifier:@"collectionViewCell"] ;
    
    
    
    _collcetionView.backgroundColor = [UIColor clearColor];
    
    //在这个地方进行头部视图复用注册
    //    UICollectionElementKindSectionHeader 头部表示kind
    //    UICollectionElementKindSectionFooter 尾部表示kind
    
    [_collcetionView registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"] ;
    
    
}


#pragma mark 计算选定月份天数
-(void)setMonthDays
{
    [_daysArray removeAllObjects];
    _daysArray = nil;
    _daysArray = [NSMutableArray array];
    
    NSInteger days = [DBcommonUtils totaldaysInThisMonth:[NSDate date] with:_date];
    NSInteger week  = [DBcommonUtils firstWeekdayInThisMonth:[NSDate date] with:_date];
    
    NSString *mydata =[NSString stringWithFormat:@"%@",_date];
    
    
    NSInteger weeks;
    if ((days + week) % 7 > 0)
    {
        weeks = (days + week)/7 + 1 ;
    }
    else
    {
        weeks = (days + week)/7 ;
    }
    
    for (int i  = 0; i < (days + week); i ++)
    {
        if ( i - week < 0)
        {
            DBModel *model = [[DBModel alloc]init];
            model.daynumber =@" ";
            model.isNowDay = NO;
            [_daysArray addObject:model];
        }
        else
        {
            NSString *theday = [NSString stringWithFormat:@"%.2ld",i+1-week];
            mydata = [mydata stringByReplacingCharactersInRange:NSMakeRange(8, 2) withString:theday];
            DBModel *model = [[DBModel alloc]init];
            model.daynumber =theday;
            model.isNowDay = [DBcommonUtils isToday:mydata];
            [_daysArray addObject:model];
            
            
        }
    }
    
    for (int i = 0; i<_daysArray.count%7; i++) {
        DBModel *model = [[DBModel alloc]init];
        model.daynumber =@" ";
        model.isNowDay = NO;
        [_daysArray addObject:model];
    }
    [_collcetionView reloadData];
}


#pragma mark -- UICollectionViewDelegate 代理方法

//设置每个分组中collectionViewCell的数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _daysArray.count ;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DBCollectionViewCell * cell = [_collcetionView dequeueReusableCellWithReuseIdentifier:@"collectionViewCell" forIndexPath:indexPath] ;
    
    if (cell == nil)
    {
        cell = [[DBCollectionViewCell alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth/7, ScreenWidth/7+20)];
        
    }
    DBModel *model = _daysArray[indexPath.row];
    cell.dateLabel.textColor = [UIColor blackColor];
    cell.dateLabel.text = model.daynumber;
    if (model.isNowDay) {
        cell.backview.backgroundColor = [df colorWithHexString:@"fff0dd" alpha:1];
    }else{
        cell.backview.backgroundColor = [UIColor whiteColor];
    }
    cell.alertImageV.image = [UIImage imageNamed:((indexPath.row==7||indexPath.row==10)?@"对号.png":(indexPath.row==15?@"礼物.png":@""))];
    
    return cell ;
}



- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    //    判断是否是头部视图
    if ([kind isEqualToString:UICollectionElementKindSectionHeader])
    {
        //从复用池中去头部视图
        HeaderCollectionReusableView * headerView = [_collcetionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath] ;
        //        headerView.backgroundColor = [UIColor yellowColor] ;
        if (headerView == nil)
        {
            headerView =[[HeaderCollectionReusableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenWidth/7*2)];
        }
        
        headerView.dateLabel.text =[_date substringWithRange:NSMakeRange(0, 10)];
        headerView.userInteractionEnabled = YES;
        [headerView.lastBt addTarget:self action:@selector(lastBt) forControlEvents:UIControlEventTouchUpInside];
        
        [headerView.nextBt addTarget:self action:@selector(nextBt) forControlEvents:UIControlEventTouchUpInside];
        
        return headerView ;
        
        
    }
    else
        return nil ;
}


#pragma mark 点击上个月

-(void)lastBt
{
    
    
    NSInteger month = [[_date substringWithRange:NSMakeRange(5,2)]integerValue];
    NSInteger year = [[_date substringWithRange:NSMakeRange(0,4)]integerValue];
    if ( month > 1 )
    {
        _date = [_date stringByReplacingCharactersInRange:NSMakeRange(5,2) withString:[NSString stringWithFormat:@"%.2ld",month - 1]];
        
    }
    else
    {
        _date = [_date stringByReplacingCharactersInRange:NSMakeRange(0,7) withString:[NSString stringWithFormat:@"%ld-12",year - 1]];
    }
    
    [self setMonthDays];
    
}


#pragma mark 点击下个月
-(void)nextBt
{
    NSInteger month = [[_date substringWithRange:NSMakeRange(5,2)]integerValue];
    NSInteger year = [[_date substringWithRange:NSMakeRange(0,4)]integerValue];
    
    if ( month < 12)
    {
        _date = [_date stringByReplacingCharactersInRange:NSMakeRange(5,2) withString:[NSString stringWithFormat:@"%.2ld",month + 1]];
    }
    else
    {
        _date = [_date stringByReplacingCharactersInRange:NSMakeRange(0,7) withString:[NSString stringWithFormat:@"%ld-01",year + 1]];
        
    }
    
    [self setMonthDays];
    
    
}


//点击某个cell会调用此函数
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"列%ld---行%ld",indexPath.section,indexPath.row) ;
}
#pragma 统计

- (UIView *)setHeaderView{
    
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KCONTROLLERWith, 134)];
    backView.backgroundColor = [UIColor whiteColor];
    CGFloat imagesize = KCONTROLLERWith*0.088;
    CGFloat pkHeight = KCONTROLLERWith*0.06;
    CGFloat pkWidth  = pkHeight*1.9286;
    CGFloat top = 15;
    CGFloat space = 16;
    
    UIImageView *pkimageV = [[UIImageView alloc]initWithFrame:CGRectMake((KCONTROLLERWith-pkWidth)/2, top+KCONTROLLERWith*0.014, pkWidth, pkHeight)];
    pkimageV.image = [UIImage imageNamed:@"PK.png"];
    [backView addSubview:pkimageV];
    
    _userLogoImageV = [[UIImageView alloc]initWithFrame:CGRectMake(pkimageV.LC_x-space-imagesize, top, imagesize, imagesize)];
    _userLogoImageV.image = [UIImage imageNamed:@"其他用户.png"];
    [backView addSubview:_userLogoImageV];
    
    UIImageView *quanImageV = [[UIImageView alloc]initWithFrame:CGRectMake(pkimageV.LC_x+pkimageV.LC_width+space, top, imagesize, imagesize)];
    quanImageV.image = [UIImage imageNamed:@"其他用户.png"];
    [backView addSubview:quanImageV];
    
    self.titleLabel = [UILabel initWithFrame:CGRectMake(0, KVIEW_Y(_userLogoImageV)+12, KCONTROLLERWith, 30) text:@"连续转入天数击败62%的用户" font:14 textAlignment:NSTextAlignmentCenter texcolor:[df colorWithHexString:@"ff8a18" alpha:1] backgroundColor:nil];
    [backView addSubview:self.titleLabel];
    [df fuwenbenLabel:self.titleLabel FontNumber:[UIFont systemFontOfSize:12] AndRange1:NSMakeRange(0, 8) AndRange2:NSMakeRange(self.titleLabel.text.length-3, 3) AndColor1:[df colorWithHexString:@"666666" alpha:1] AndColor2:[df colorWithHexString:@"666666" alpha:1]];
    CGFloat titleImagesize = 20;
    CGFloat labelHeight = 44;
    UIImageView *titleImageV= [[UIImageView alloc]initWithFrame:CGRectMake(20, 90+(labelHeight-titleImagesize)/2, titleImagesize, titleImagesize)];
    titleImageV.image = [UIImage imageNamed:@"奖杯.png"];
    [backView addSubview:titleImageV];
    
    UILabel *theLabel = [UILabel initWithFrame:CGRectMake(KVIEW_X(titleImageV)+10, 90, KCONTROLLERWith, labelHeight) text:@"连续转入前十名" font:12 textAlignment:NSTextAlignmentLeft texcolor:[df colorWithHexString:@"666666" alpha:1] backgroundColor:nil];
    [backView addSubview:theLabel];
    return backView;
}

- (void)layoutTableView{
    
    _tableview = [[UITableView alloc]initWithFrame:CGRectMake(KCONTROLLERWith, 0, KCONTROLLERWith, KCONTROLLERHeight-KCONTROLLERWith*0.5334) style:UITableViewStylePlain];
    _tableview.backgroundColor = [UIColor whiteColor];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    [bottomScrollview addSubview:_tableview];
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableview.tableHeaderView = [self setHeaderView];
    [self setUpTableView];
    //    view.backgroundColor = RGBACOLOR(241, 241, 241, 1);
}

- (void)setUpTableView{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [_tableview addHeaderWithTarget:self action:@selector(headerRereshing)];
    
    // 上拉加载
    [_tableview addFooterWithTarget:self action:@selector(footerRereshing)];
    
}
- (void)headerRereshing{
    // 2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(KRefreshTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //getMarriageSeeking(网络请求)
        //        [self theRequestWithKeyWord:mykeyword];
    });
}
- (void)footerRereshing{
    // 2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(KRefreshTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //getMarriageSeeking(网络请求)
        //        [self loadMoreDataRequestWithKeyWord:mykeyword];
        
    });
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *cellid = [NSString stringWithFormat:@"textfield%ld",(long)indexPath.row];
    NewTongJiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell == nil) {
        cell = [[NewTongJiTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid tongJiCellType:indexPath.row>2?NumberCellType:ImageCellType CellSata:0];
    }
    [cell setValueWithModel:indexPath];
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
